// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.conversationkit.ui.view;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.netease.yunxin.kit.common.ui.utils.AvatarColor;
import com.netease.yunxin.kit.common.ui.utils.TimeFormatUtils;
import com.netease.yunxin.kit.common.ui.viewholder.BaseViewHolder;
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient;
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig;
import com.netease.yunxin.kit.conversationkit.ui.R;
import com.netease.yunxin.kit.conversationkit.ui.common.ConversationUtils;
import com.netease.yunxin.kit.conversationkit.ui.databinding.P2pViewHolderLayoutBinding;
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean;

public class P2PViewHolder extends BaseViewHolder<ConversationBean> {

  private P2pViewHolderLayoutBinding viewBinding;

  public P2PViewHolder(@NonNull P2pViewHolderLayoutBinding binding) {
    super(binding.getRoot());
    viewBinding = binding;
  }

  @Override
  public void onBindData(ConversationBean data, int position) {
    loadUIConfig();
    if(ConversationAdapter.imOfficialBean != null && data.param.equals("10000001")){
      viewBinding.conversationNameTv.setText(ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getName());
      viewBinding.avatarView.setData(
              "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/"+ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getIcon(),
              data.infoData.getName(),
              AvatarColor.avatarColor(data.infoData.getContactId()));
    }else {
      viewBinding.conversationNameTv.setText(data.infoData.getName());
      viewBinding.avatarView.setData(
              data.infoData.getAvatar(),
              data.infoData.getName(),
              AvatarColor.avatarColor(data.infoData.getContactId()));
    }

    viewBinding.rootView.setBackground(viewBinding.getRoot().getContext().getDrawable(R.drawable.view_normal_selector));
    if (data.infoData.getMute()) {
      viewBinding.conversationMuteIv.setVisibility(View.VISIBLE);
      viewBinding.conversationUnreadTv.setVisibility(View.GONE);
    } else {
      viewBinding.conversationMuteIv.setVisibility(View.GONE);
      if(ConversationAdapter.imOfficialBean != null && data.param.equals("10000001")){
        if (ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getNewestMsg().getNotRead() > 0) {
          int count = ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getNewestMsg().getNotRead();
          String content;
          if (count >= 100) {
            content = "99+";
          } else {
            content = String.valueOf(count);
          }
          viewBinding.conversationUnreadTv.setText(content);
          viewBinding.conversationUnreadTv.setVisibility(View.VISIBLE);
        } else {
          viewBinding.conversationUnreadTv.setVisibility(View.GONE);
        }
      }else {
        if (data.infoData.getUnreadCount() > 0) {
          int count = data.infoData.getUnreadCount();
          String content;
          if (count >= 100) {
            content = "99+";
          } else {
            content = String.valueOf(count);
          }
          viewBinding.conversationUnreadTv.setText(content);
          viewBinding.conversationUnreadTv.setVisibility(View.VISIBLE);
        } else {
          viewBinding.conversationUnreadTv.setVisibility(View.GONE);
        }
      }
    }
    if(ConversationAdapter.imOfficialBean != null && data.param.equals("10000001")){
      viewBinding.conversationMessageTv.setText(ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getNewestMsg().getMessage().getContent());
    }else {
      viewBinding.conversationMessageTv.setText(
              ConversationUtils.getConversationText(itemView.getContext(), data.infoData));
    }
    viewBinding.conversationTime.setText(
        TimeFormatUtils.formatMillisecond(
            viewBinding.getRoot().getContext(), data.infoData.getTime()));
    viewBinding.conversationTimeFl.setOnClickListener(v -> {
      if(ConversationAdapter.imOfficialBean != null && data.param.equals("10000001")){
        LiveEventBus.get("startOfficialIm").post(null);
        ConversationAdapter.imOfficialBean.getData().getOfficialMsgInfo().getNewestMsg().setNotRead(0);
        LiveEventBus.get("RefreshNoReading").post(null);
        viewBinding.conversationUnreadTv.setText("0");
        viewBinding.conversationUnreadTv.setVisibility(View.GONE);
      }else {
        itemListener.onClick(data, position);
      }
    });
    viewBinding.conversationTimeFl.setOnLongClickListener(
        v -> itemListener.onLongClick(data, position));
    viewBinding.conversationAvatarFl.setOnClickListener(
        v -> {
          if(!data.param.equals("10000001") && !data.param.equals("100000")){
            LiveEventBus.<String>get("startDetailsActivity").post(data.param.toString());
          }
        });
    viewBinding.conversationAvatarFl.setOnLongClickListener(
        v -> itemListener.onAvatarLongClick(data, position));
  }

  private void loadUIConfig() {
    if (ConversationKitClient.getConversationUIConfig() != null) {
      ConversationUIConfig config = ConversationKitClient.getConversationUIConfig();
      if (config.itemTitleColor != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationNameTv.setTextColor(config.itemTitleColor);
      }
      if (config.itemTitleSize != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationNameTv.setTextSize(config.itemTitleSize);
      }

      if (config.itemContentColor != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationMessageTv.setTextColor(config.itemContentColor);
      }
      if (config.itemContentSize != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationMessageTv.setTextSize(config.itemContentSize);
      }

      if (config.itemDateColor != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationTime.setTextColor(config.itemDateColor);
      }
      if (config.itemDateSize != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.conversationTime.setTextSize(config.itemDateSize);
      }

      if (config.avatarCornerRadius != ConversationUIConfig.INT_DEFAULT_NULL) {
        viewBinding.avatarView.setCornerRadius(config.avatarCornerRadius);
      }
    }
  }
}
